package com.globallogic.training.javase.university.member;

import java.util.Date;

/**
 * Abstract class containing collective information about any person, like name,
 * date of birth etc.
 * <p>
 * Assumption: person is unique identified by the full name.
 * 
 * @author Artem.Zeltinsh
 */
public abstract class Person {

	private final String firstName;

	private final String lastName;

	private Date dateOfBirth;

	private String placeOfBirth;

	public Person(String fullName, Date dateOfBirth) {

		if (fullName == null) {
			throw new IllegalArgumentException(
					"Input 'fullName' should not be null");
		}

		String[] names = fullName.trim().split("\\s+");
		this.firstName = names[0];
		this.lastName = names[names.length - 1];
		this.dateOfBirth = dateOfBirth;
	}

	public String getFirstName() {
		return firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public String getFullName() {
		return firstName + " " + lastName;
	}

	public Date getDateOfBirth() {
		return dateOfBirth;
	}

	public String getPlaceOfBirth() {
		return placeOfBirth;
	}

	public void setPlaceOfBirth(String placeOfBirth) {
		this.placeOfBirth = placeOfBirth;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + firstName.hashCode();
		result = prime * result + lastName.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}

		if (getClass() != obj.getClass()) {
			return false;
		}
		Person other = (Person) obj;
		if (firstName.equals(other.firstName)
				&& lastName.equals(other.lastName)) {
			return true;
		}
		return false;
	}

	@Override
	public String toString() {
		return firstName + " " + lastName;
	}
}